package com.ddstation.onsitelaw.service;


import com.ddstation.onsitelaw.entity.*;
import com.ddstation.onsitelaw.mapper.DtInvsManageInstitutionMapper;
import com.ddstation.onsitelaw.mapper.DtInvsManageInstitutionSupervisorMapper;
import com.ddstation.onsitelaw.mapper.DtInvsSupervisorMapper;
import com.ddstation.onsitelaw.model.AccountModel;
import com.ddstation.onsitelaw.utils.Constant;
import com.ddstation.onsitelaw.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 15093
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Inject
    private DtInvsManageInstitutionSupervisorMapper institutionDoctorBelowMapper;

    @Inject
    private DtInvsManageInstitutionSupervisorMapper dtInvsManageInstitutionSupervisorMapper;

    @Inject
    private DtInvsManageInstitutionMapper dtInvsManageInstitutionMapper;

    @Inject
    private DtInvsSupervisorMapper dtInvsSupervisorMapper;

    @Override
    @Transactional(readOnly = true)
    public AccountModel login(String loginUser, String loginPassword) {
        Example example = new Example(DtInvsManageInstitutionSupervisorEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginUser", loginUser);
        criteria.andEqualTo("loginPassword", loginPassword);
        //状态，0：可用
        criteria.andEqualTo("status", 0);
        List<DtInvsManageInstitutionSupervisorEntity> userList = institutionDoctorBelowMapper.selectByExample(example);
        AccountModel accountModel = null;
        if (!ObjectUtils.isEmpty(userList)) {
            accountModel = new AccountModel();
            DtInvsManageInstitutionSupervisorEntity user = userList.get(0);
            DtInvsSupervisorEntity supervisor = dtInvsSupervisorMapper.selectByPrimaryKey(user.getSupervisorId());
            BeanUtils.copyProperties(user, accountModel);
            BeanUtils.copyProperties(supervisor, accountModel);
        }
        return accountModel;

    }

    @Override
    public String judgeIsMobile(HttpServletRequest request) {
        // 从浏览器获取请求头信息
        String info = request.getHeader("user-agent");
        if (info.contains("Windows")) {
            return Constant.PC;
        }

        if (info.contains("Macintosh")) {
            return Constant.PC;
        }

        if (info.contains("Android")) {
            return Constant.APP;
        }

        if (info.contains("iPhone")) {
            return Constant.APP;
        }

        if (info.contains("iPad")) {
            return Constant.APP;
        }
        return Constant.PC;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePassword(Integer supervisorId, String newPassword) {
        institutionDoctorBelowMapper.updatePassword(supervisorId, newPassword);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInstitutionId(Integer supervisorId, String institutionId) {
        institutionDoctorBelowMapper.updateInstitutionId(supervisorId, institutionId);
    }

    @Override
    @Transactional(readOnly = true)
    public AccountModel selectById(Integer id) {
        return institutionDoctorBelowMapper.selectById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public AccountModel selectByLoginUser(String loginUser) {
        return institutionDoctorBelowMapper.selectByLoginUser(loginUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {
        return institutionDoctorBelowMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSupervisor(AccountSaveEntity saveEntity) {
        institutionDoctorBelowMapper.saveSupervisor(saveEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInstitutionSupervisor(AccountSaveEntity saveEntity) {
        institutionDoctorBelowMapper.saveInstitutionSupervisor(saveEntity);
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult selectSupervisors(HashMap paramMap) {
        int pageNum = Integer.parseInt(paramMap.get("pageNum").toString());
        int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<AccountModel> supervisorList = dtInvsManageInstitutionSupervisorMapper.selectSupervisors(paramMap);
        PageInfo<AccountModel> info = new PageInfo<>(supervisorList);
        return new PageResult(info, supervisorList);
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult selectManagerAndSupervisors(HashMap paramMap) {
        int pageNum = Integer.parseInt(paramMap.get("pageNum").toString());
        int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());

        Example example = new Example(DtInvsManageInstitutionEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("province", paramMap.get("province"));
        criteria.andEqualTo("city", paramMap.get("city"));
        criteria.andIsNotNull("county");
        criteria.andNotEqualTo("county", "");
        List<DtInvsManageInstitutionEntity> invsItutionList = dtInvsManageInstitutionMapper.selectByExample(example);
        List<String> institutionIdList = invsItutionList.stream().map(i -> i.getId()).collect(Collectors.toList());
        paramMap.put("institutionIdList", institutionIdList);
        PageHelper.startPage(pageNum, pageSize);
        List<AccountModel> supervisorList = dtInvsManageInstitutionSupervisorMapper.selectManagerAndSupervisors(paramMap);
        PageInfo<AccountModel> info = new PageInfo<>(supervisorList);
        return new PageResult(info, supervisorList);
    }

}